
function validate()
{	
	
	//getting user details	
	var use_detail = document.getElementById("user_form");	 
	var cus_name = use_detail.name.value;	
	var phone_num = use_detail.phone_num.value;	
	var unit_num = use_detail.unit_num.value;	
	var st_num = use_detail.st_num.value;		
	var st_name = use_detail.st_name.value;		
	var subrub = use_detail.subrub.value;		
	var ds_subrub = use_detail.ds_subrub.value;
	
	//getting timings for pickup
	var DD = use_detail.DD.value;	
	var MM = use_detail.MM.value;
	var YY = use_detail.YY.value;
	var HH = use_detail.HH.value;
	var min = use_detail.MM.value;
	var meridian = use_detail.meridiem.value;
	
	
	var xhr = createRequest(); 					//calling createRequest function to create XHR object
	alert("connection sucessful");
	var alphaReg = /^[a-zA-Z]+$/;				//'alphaReg' variable used to check valid user name or not
	var numReg = /^[0-9]+$/;					//'numrReg' variable used to check valid user phone number or not
	
	/*validating user input*/
	if(cus_name.match(alphaReg) == null)			
	{
		alert("Only alphabets are allowed for name");
		user_form.name.focus();
		return false;
	}	
	if(phone_num.match(numReg) == null)
	{
		alert("Only numbers are allowed in Phone Number");
		user_form.phone_num.focus();
		return false;
	}	
	if(st_num == "")
	{
		alert("enter street number please");		
		return false;
	}
	if(st_name == "")
	{
		alert("enter street name please");		
		return false;
	}	
	if(subrub == "")
	{
		alert("enter pickup subrub please");		
		return false;
	}
	if(ds_subrub == "")
	{
		alert("enter destination subrub please");		
		return false;
	}
	
	/*Checking user entry date*/
	var currentTime = new Date();
	var day = currentTime.getDate();
	var month = currentTime.getMonth();
	var year = currentTime.getFullYear();
	var hours = currentTime.getHours();
	var minutes = currentTime.getMinutes();
	
	if( (DD < day) || (MM < month) || (YY < year))
	{
		alert("PLease enter correct date");
		return false;
	}	
	else if(meridian == 'AM')
	{		
		
		if( HH < 12)
		{
			alert("enter correct time");
			return false;
		}		
	}
	
	//Using XHR object
	if(xhr) 
	{
		alert("connection sucessful");
		var obj = document.getElementById(targetDivBook); 
		var requestbody ="cus_name="+encodeURIComponent(cus_name);
		/*"&phone_num="+encodeURIComponent(phone_num)+"st_num="+encodeURIComponent(st_num)+"st_name="
		encodeURIComponent(st_name);*/
		
		xhr.open("POST", serve.php, true); 
		xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
		xhr.onreadystatechange = function()
		{ 
			alert(xhr.readyState); // to let us see the state of the computation 
			if (xhr.readyState == 4 && xhr.status == 200) 
			{ 
				obj.innerHTML = xhr.responseText; 
			} // end if 
		} // end anonymous call-back function 
		 xhr.send(requestbody); 
	}
	else
	{
		alert("connection to server failed");
	}
}

function createRequest()	// function that creates XHR object
 {
	
    var xhr = false;  
    if (window.XMLHttpRequest) 
	{
        xhr = new XMLHttpRequest();		
    }
	
    else if (window.ActiveXObject)
	{
        xhr = new ActiveXObject("Microsoft.XMLHTTP");		
    }
    return xhr;
} 